//
//  ComPassVC.m
//  TT
//
//  Created by Lenny on 16/4/21.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "ComPassVC.h"

@interface ComPassVC ()<UITextFieldDelegate>
{
    CGFloat _textWide,_textBetweenW,_originX;
    NSMutableArray *_nameS;
}


@end

@implementation ComPassVC

#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"组队";
    self.view.backgroundColor = WL_Back_Gray_Color;
    [self setNaviBar];
    [self setData];
    [self UI];
    
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = NO;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
}

#pragma mark - event methods
- (void)goBack {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - delegate methods

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSInteger index = textField.text.length;

    if ([string isEqualToString:@""]) {
        
//        ((UILabel *)_nameS[index - 1]).text = @"";
        ((UILabel *)_nameS[index - 1]).subviews[0].hidden = YES;
    }else {
        
        if (index >= _nameS.count) return NO;
        
         ((UILabel *)_nameS[index]).subviews[0].hidden = NO;
//        ((UILabel *)_nameS[index]).text = string;
        
        if (index == 5) {
            
            NSString *passStr = [NSString stringWithFormat:@"%@%@",textField.text,string];
            NSLog(@"%@",passStr);
            [self goBack];
        }
    }
    return YES;
}

#pragma mark - private methods
- (void)setNaviBar{
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [leftBtn setImage:[UIImage imageNamed:@"icon_navi_back"] forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setData {
    
    _textBetweenW = 10;
    _textWide = 45;
    _originX = (k_screen_w - (_textWide * 6 + _textBetweenW * 5)) / 2;
    
    _nameS = [[NSMutableArray alloc] init];
}

- (void)UI {

    
    UITextField *textField = [UITextField new];
    [self.view addSubview:textField];
    [textField becomeFirstResponder];
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.delegate = self;
    textField.secureTextEntry = YES;
    
    for (int i = 0 ; i < 6; i ++) {

        UILabel *nameL = [[UILabel alloc] initWithFrame:CGRectMake(_originX + (_textWide + _textBetweenW) * i, _originX, _textWide, _textWide)];
        nameL.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:nameL];
        nameL.text = @"";
        nameL.textAlignment = NSTextAlignmentCenter;
        [_nameS addObject:nameL];
        
        UIView *hitV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        hitV.center = CGPointMake(_textWide / 2, _textWide / 2);
        hitV.layer.cornerRadius = 5;
        hitV.backgroundColor = [UIColor blackColor];
        [nameL addSubview:hitV];
        hitV.hidden = YES;
    }
}


@end
